/*
@ClassName JobScheduleController
@Description TODO
@Author SubUp
@Date   2022/7/31
@Version 1.0
*/

package com.sunjob.mrojobschedule.controller;

import com.sunjob.common.domain.scheduleServer.JobSchedule;
import com.sunjob.common.domain.scheduleServer.orm.JobResult;
import com.sunjob.common.mroAnno.ControllerLog;
import com.sunjob.common.mroAnno.LogModuleType;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mrojobschedule.service.JobScheduleService;
import com.sunjob.mrojobschedule.service.impl.JobScheduleServiceImpl;
import com.sunjob.mrojobschedule.util.ScheduleUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "作业调度")
@RequestMapping("/job/")
public class JobScheduleController {
    @Resource
    private JobScheduleService jobScheduleService;

    @GetMapping("list/{pageId}")
    @ResponseBody
//    @ApiOperation(value = "查看所有的原始作业")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<JobSchedule>> showJob(@PathVariable String pageId)
    {
        List<JobSchedule> jobList = jobScheduleService.selectJobList(pageId);
        if (jobList.size() == 0)
            return Result.build(null,200,"no query primitive job by param");
//            return Result.fail("query primitive job failure by param");
        return Result.ok(jobList,"query primitive job success by param");
    }

    @GetMapping("list/orm/{pageId}")
    @ApiOperation(value = "作业调度界面展示作业管理数据")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<JobResult>> showJobOrm(@ApiParam(value = "分页 作业编号",defaultValue = "1") @PathVariable String pageId){
        List<JobResult> jobResults = jobScheduleService.selectJobOrmList(pageId);
        if (jobResults.size() == 0)
            return Result.build(null,200,"query  job failure by param");
        return Result.ok(jobResults,"query  job success by param");
    }

    @GetMapping("query/orm/{jobId}/{pageId}")
    @ApiOperation(value = "作业编号展示作业信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<JobResult>> selectJobResultById(@ApiParam(value = "作业编号",required = true) @PathVariable String jobId,
            @ApiParam(value = "分页 编号",defaultValue = "1") @PathVariable(required = false) String pageId){
        List<JobResult> jobResults = jobScheduleService.selectJobById(jobId,pageId);
        if(jobResults.size() !=0)
            return Result.build(null,200,"query job message failure");
        return Result.ok(jobResults,"query job message success");
    }
    @GetMapping("query/orm/level/{jobLevel}/{pageId}")
    @ApiOperation(value = "作业级别展示作业信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<JobResult>> selectJobResultByJobLevel(@ApiParam(value = "作业级别",required = true) @PathVariable String jobLevel,
                                                             @ApiParam(value = "分页编号",defaultValue = "1") @PathVariable String pageId){
        List<JobResult> jobResults = jobScheduleService.selectJobOrmByJobLevel(jobLevel,pageId);
        if(jobResults.size() == 0)
            return Result.build(null,200,"query job message failure by search jobLevel");
        return Result.ok(jobResults,"query job message success by search jobLevel");
    }

    @GetMapping("query/orm/feedback/{jobFeedBack}/{pageId}")
    @ApiOperation(value = "作业反馈状态展示作业信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<JobResult>> selectJobResultByJobFeedBack(@ApiParam(value = "作业级别",required = true) @PathVariable String jobFeedBack,
                                                                @ApiParam(value = "分页编号",defaultValue = "1") @PathVariable String pageId){
        List<JobResult> jobResults = jobScheduleService.selectJobOrmByJobFeedBack(jobFeedBack,pageId);
        if(jobResults.size() == 0)
            return Result.build(null,200,"query job message failure by search jobFeedBack");
        return Result.ok(jobResults,"query job message success by search jobFeedBack");
    }

    @GetMapping("query/orm/number/{staffId}/{pageId}")
    @ApiOperation(value = "技术员工号展示作业信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<JobResult>> selectJobResultByJobStaffId(@ApiParam(value = "技术员工号",required = true) @PathVariable String staffId,
                                                               @ApiParam(value = "分页编号",defaultValue = "1") @PathVariable String pageId){
        List<JobResult> jobResults = jobScheduleService.selectJobOrmByStaffId(staffId,pageId);
        if(jobResults.size() == 0)
            return Result.build(null,200,"query job message failure by search staffId");
        return Result.ok(jobResults,"query job message success by search staffId");
    }

    @GetMapping("query/orm/staff/{staffName}/{pageId}")
    @ApiOperation(value = "技术员姓名展示作业信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<JobResult>> selectJobResultByJobStaffName(@ApiParam(value = "技术员姓名",required = true) @PathVariable String staffName,
                                                                 @ApiParam(value = "分页编号",defaultValue = "1") @PathVariable String pageId){
        List<JobResult> jobResults = jobScheduleService.selectJobOrmByStaffName(staffName,pageId);
        if(jobResults.size() == 0)
            return Result.build(null,200,"query job message failure by search staffName");
        return Result.ok(jobResults,"query job message success by search staffName");
    }

    @GetMapping("/query/records/{jobLevel}/{jobFeedBack}")
    @ApiOperation(value = "作业记录条数")
    @ControllerLog(LogModuleType.PLAN)
    public Integer countRecordsByJob(@ApiParam(value = "作业级别") @PathVariable(required = false) String jobLevel,
                                     @ApiParam(value = "作业反馈结果") @PathVariable(required = false) String jobFeedBack){
        Integer rt = jobScheduleService.selectJobCount(jobLevel,
                jobFeedBack);
        return rt;
    }

    @PostMapping("save/{jobObj}")
    @ApiOperation(value = "派发作业")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Integer> addSave(@ApiParam(value = "作业信息",required = true) @RequestBody JobSchedule jobObj){
        int rt = jobScheduleService.insertJob(jobObj);
        if(rt == 0){
            return Result.build(null,200,"insert job failure");
        }
        return Result.ok(rt,"insert job success");
    }

    @PostMapping("edit/{jobObj}")
    @ApiOperation(value = "修改作业")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Integer> editSave(@ApiParam(value = "作业信息",required = true) @RequestBody JobSchedule jobObj){
        int rt = jobScheduleService.updateJob(jobObj);
        if(rt == 0)
            return Result.build(rt,200,"update primitive job failure");
        return Result.ok(rt,"update primitive job success");
    }
    @PostMapping("edit/orm/{jobObj}")
    @ApiOperation(value = "修改作业")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Integer> editJobSave(@ApiParam(value = "作业信息",required = true) @RequestBody JobResult jobObj){
        int rt = jobScheduleService.updateJobResult(jobObj);
        if(rt == 0)
            return Result.build(rt,200,"update job failure");
        return Result.ok(rt,"update job success");
    }
    @PostMapping("edit/feedback/{jobId}/{jobFeedBack}")
    @ApiOperation(value = "修改作业反馈状态")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Integer> editJobFeedBackByJobId(@ApiParam(value = "作业编号",required = true) @PathVariable(required = true) String jobId,
                                                  @ApiParam(value = "作业反馈状态",required = true) @PathVariable(required = true) String jobFeedBack){
        Integer rt = jobScheduleService.updateJobFeedBack(jobId,jobFeedBack);
        if (rt == 0)
            return Result.build(rt,200,"edit primitive job message failure");
        return Result.ok(rt,"edit primitive job message success");
    }

    @PostMapping("edit/level/{jobId}/{jobLevel}")
    @ApiOperation(value = "修改作业作业级别")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Integer> editJobLevelByJobId(@ApiParam(value = "作业编号",required = true) @PathVariable(required = true) String jobId,
                                                  @ApiParam(value = "作业反馈状态",required = true) @PathVariable(required = true) String jobLevel){
        Integer rt = jobScheduleService.updateJobLevel(jobId, jobLevel);
        if (rt == 0)
            return Result.build(rt,200,"edit primitive job message failure");
        return Result.ok(rt,"edit primitive job message success");
    }
    @GetMapping("remove/{jobId}")
    @ApiOperation(value = "删除作业")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Integer> remove(@ApiParam(value = "作业id",required = true) @PathVariable String jobId)
    {
        int rt = jobScheduleService.deleteJob(jobId);
        if (rt == 0)
            return Result.build(rt,200,"delete job failure");
        return Result.ok(rt,"delete job success");
    }

}
